package com.masterclient.app.modules.goods.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.goods.adapter.GoodsCommentAdapter;
import com.eoner.baselibrary.bean.goods.CommentData;
import com.eoner.baselibrary.bean.goods.CommentMessage;
import com.eoner.baselibrary.bean.goods.CommentTabModel;
import com.masterclient.app.modules.goods.contract.CommentContract;
import com.masterclient.app.modules.goods.contract.CommentPresenter;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class CommentFragment extends BaseFragment<CommentPresenter> implements CommentContract.View {


    @BindView(R.id.bottom_layout)
    View bottom_layout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private String product_id,type;
    private GoodsCommentAdapter commentAdapter;
    private View footView;
    private List<CommentMessage> list;
    private int total_row;

    private int page = 1;
    boolean hasMore = true;

    public static CommentFragment getInstance(String product_id,String type) {
        CommentFragment timeLimitedBuyFragment = new CommentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("product_id", product_id);
        bundle.putString("type", type);
        timeLimitedBuyFragment.setArguments(bundle);
        return timeLimitedBuyFragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.comment_fragment, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        this.product_id = (String) getArguments().get("product_id");
        this.type = (String) getArguments().get("type");
        footView = View.inflate(mContext,R.layout.view_foot4,null);
        commentAdapter = new GoodsCommentAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(commentAdapter);

        commentAdapter.bindToRecyclerView(recyclerView);
        //commentAdapter.setFooterView(footView);


        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        commentAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if(hasMore){
                    mPresenter.getCommentList(product_id,type,"" + page);
                }
            }
        }, recyclerView);

    }


    boolean isFirst = true;
    @Override
    public void onResume() {
        super.onResume();
        isFirst = false;
        refresh();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser && !isFirst){
            refresh();
        }
    }

    private void refresh() {
        if(list != null){
            list.clear();
            commentAdapter.notifyDataSetChanged();
        }
        footView.setVisibility(View.GONE);
        page = 1;
        mPresenter.getCommentList(product_id,type,"" + page);
    }


    @Override
    public void showGoodsComment(CommentData data) {
        if(swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }

        hasMore = data.isHas_more();

        if(list == null){
            list = new ArrayList<>();
        }

        list.addAll(data.getReview_list());
        commentAdapter.setNewData(list);
        if(hasMore){
            page ++;
            footView.setVisibility(View.GONE);
        }else {
            commentAdapter.setEnableLoadMore(false);
            footView.setVisibility(View.VISIBLE);
        }
        commentAdapter.notifyDataSetChanged();
    }

    @Override
    public void getCommentListFail(String msg) {
        if (swipeLayout != null && swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
        showToast(msg);
    }

    @Override
    public void getTabDataSuccess(List<CommentTabModel> commentTabModels) {

    }

    @Override
    public void getTabDataFail(String msg) {

    }
}
